package com.example.cardgame

import android.app.AlertDialog
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.graphics.drawable.toBitmap
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.cardgame.databinding.ActivityFirstBinding
import java.io.ByteArrayOutputStream


//不同intent传递参数
const val IMG_MSG="IMG MSG"
const val NAME_MSG="NAME MSG"
const val SEX_MSG="SEX MSG"
const val SNO_MSG="SNO MSG"
const val TEL_MSG="TEL MSG"
const val EMAIL_MSG="EMAIL MSG"
const val HOBBY_MSG="HOBBY MSG"
const val SCORE_MSG="SCORE MSG"

class FirstActivity : ComponentActivity() {
    private  val binding by lazy {
        ActivityFirstBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        //用于传会修改信息页面的数据
        val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {result->
            if(result.resultCode == 1){
                binding.textname.text=result.data?.getStringExtra(NAME_MSG)
                binding.textsex.text=result.data?.getStringExtra(SEX_MSG)
                binding.textsno.text=result.data?.getStringExtra(SNO_MSG)
                binding.texttel.text=result.data?.getStringExtra(TEL_MSG)
                binding.textemail.text=result.data?.getStringExtra(EMAIL_MSG)
                //多个爱好数据传回
                val hobbies = result.data?.getStringArrayListExtra(HOBBY_MSG)
                if (hobbies != null) {
                    val hobbiesText = hobbies.joinToString(", ")
                    binding.texthobby.text=hobbiesText
                }
                //用于头像的传回，以bytearray形式传回，在转换为bitmap放入imageview
                val bitmapBytes = result.data?.getByteArrayExtra(IMG_MSG)
                val bitmap = bitmapBytes?.let { BitmapFactory.decodeByteArray(bitmapBytes, 0, it.size) }
                binding.image.setImageBitmap(bitmap)
            }

        }

        //修改信息按钮的监听
        binding.modify.setOnClickListener{
            val intent = Intent(this,SecondActivity::class.java)
            //多个参数传出，通过使用bundle实现
            val extras = Bundle()
            extras.putString(NAME_MSG,binding.textname.text.toString())
            extras.putString(SEX_MSG,binding.textsex.text.toString())
            extras.putString(SNO_MSG,binding.textsno.text.toString())
            extras.putString(TEL_MSG,binding.texttel.text.toString())
            extras.putString(EMAIL_MSG,binding.textemail.text.toString())
            //字符串传到checkbox，先分割，以数组形式传递
            val hobbiesText=binding.texthobby.text.toString()
            val hobbiesList=hobbiesText.split(",").filter { it.isNotEmpty() }
            extras.putStringArrayList(HOBBY_MSG,ArrayList(hobbiesList))
            //imageview转换为bitmap数组再传递
            if (binding.image.drawable != null) {
                    val bitmap = binding.image.drawable.toBitmap()
                    // 将Bitmap转换为byte[]数组
                    val byteArrayOutputStream = ByteArrayOutputStream()
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream)
                    val bitmapBytes = byteArrayOutputStream.toByteArray()
                    extras.putByteArray(IMG_MSG,bitmapBytes)
            }
            intent.putExtras(extras)
            launcher.launch(intent)
        }
        //最高分传回信息页面
        val launcher_game = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {result->
            if(result.resultCode == 2) {
                binding.texthighest.text = result.data?.getStringExtra(SCORE_MSG)
            }
            }
        //游戏按钮的监听
        binding.entergame.setOnClickListener{
            val intent = Intent(this,MainActivity::class.java)
            intent.putExtra(SCORE_MSG,binding.texthighest.text)
            launcher_game.launch(intent)
        }

    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        val inflater: MenuInflater = menuInflater
        inflater.inflate(R.menu.menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle item selection.
        when (item.itemId) {
            R.id.show_dialog -> {
                // 显示 AlertDialog
                showAboutDialog()
                return true
            }
            // 处理其他菜单项
            else -> return super.onOptionsItemSelected(item)
        }

    }

    private fun showAboutDialog() {
        val dialogBuilder = AlertDialog.Builder(this)
        dialogBuilder.setTitle("关于我们")
        dialogBuilder.setMessage("游戏名称:cardgame\n版本: 1.2\n开发者: SiCNU\n")
        dialogBuilder.setPositiveButton("确定") { dialog, _ -> dialog.dismiss() }
        dialogBuilder.show()
    }


}
